﻿#define DEBUG

using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm.MakingBuffer
{
    class Mushashino : Common, IRoute 
    {
        public Mushashino(soDatasets objDSs, Hashtable routeLnWithBfr, soStrings objStrFields)
            : base(objDSs, routeLnWithBfr, objStrFields)
        {
        }

        #region IRoute メンバ

        /// <summary>
        /// 武蔵野線より、バッファーを作成
        /// </summary>
        /// <param name="objVector">処理元の線路データセットベクトル</param>
        public override void MakeBufferForRoute(soDatasetVector objVector)
        {
            soRecordset objRS = null;
            soRecordset objSubRS = null;

            /*該当する処理対象を見当たる*/
            objRS = objVector.Query(
                RouteDataset.FLD_DAIBUN_CD + " = '01' and (" +
                "(" + RouteDataset.FLD_FEATURE_CD + " in ('0102', '0106') and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '201')" +
                " or " +
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0102' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '666'))",
                false, objStrFields,
                "group by " + RouteDataset.FLD_FEATURE_CD + ", " + RouteDataset.FLD_DAIBUN_CD + ", " + RouteDataset.FLD_TSUUSHO_CD);
            if (objRS != null && objRS.RecordCount > 0)
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if DEBUG
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    try
                    {
                        feature = objRS.GetFieldValue(RouteDataset.FLD_FEATURE_CD).ToString();
                        daibun = objRS.GetFieldValue(RouteDataset.FLD_DAIBUN_CD).ToString();
                        tsuusho = objRS.GetFieldValue(RouteDataset.FLD_TSUUSHO_CD).ToString();

                        Console.WriteLine(
                            String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}",
                                RouteDataset.FLD_FEATURE_CD, feature,
                                RouteDataset.FLD_DAIBUN_CD, daibun,
                                RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                j + 1, objRS.RecordCount));

                        objSubRS = objVector.Query(
                            RouteDataset.FLD_FEATURE_CD + " = '" + feature + "' and " +
                            RouteDataset.FLD_DAIBUN_CD + " = '" + daibun + "' and " +
                            RouteDataset.FLD_TSUUSHO_CD + " = '" + tsuusho + "'",
                            true, null, "");

                        int multiLineCount = objSubRS.RecordCount;
                        if (objSubRS != null && multiLineCount > 0)
                        {
                            if (!objSubRS.IsBOF())
                            {
                                objSubRS.MoveFirst();
                            }

                            /*線路はキロ程を通じたら、キロ程データセットベクトルを保留*/
                            soGeoLine objLineTmp = null;
                            soGeoLine[] objMultiLines = new soGeoLine[Utility.MAX_MULTIPLE_LINES]; //複数非連続線路
                            soDatasetVector[] objDSVsKirotei = new soDatasetVector[5];
                            objDSVsKirotei[1] = objDSs["キロ標_大宮"] as soDatasetVector;
                            objDSVsKirotei[2] = objDSs["キロ標_八王子"] as soDatasetVector;
                            objDSVsKirotei[3] = objDSs["キロ標_東京"] as soDatasetVector;
                            objDSVsKirotei[0] = objDSs["キロ標_千葉"] as soDatasetVector;
                            objDSVsKirotei[4] = objDSs["キロ標_横浜"] as soDatasetVector;
                            int i = 0;
                            int l = 0;
                            for (; !objSubRS.IsEOF(); objSubRS.MoveNext())
                            {
                                l = 0;
                                soRecordset objRSContain = null;
                                for (; l < 5; l++)
                                {
                                    objRSContain = objDSVsKirotei[l].QueryEx(objSubRS.GetGeometry(), seSpatialQueryMode.scsCommonPoint, "");
                                    if (objRSContain != null)
                                    {
                                        break;
                                    }
                                }
                                if (l == 5)
                                {
                                    continue;
                                }
                                objLineTmp = objSubRS.GetGeometry() as soGeoLine;
                                objMultiLines[i++] = objLineTmp;
                                if (objRSContain != null)
                                {
                                    Marshal.ReleaseComObject(objRSContain);
                                    objRSContain = null;
                                }
                            }
                            objSubRS.Close();
                            Marshal.ReleaseComObject(objSubRS);
                            objSubRS = null;

                            //複数非連続線路
                            for (int k = 0; k < i; k++)
                            {
                                Console.WriteLine(
                                    String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                                        RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                        k + 1, i));

                                //MakeBufferForPart(objMultiLines[k]);
                                MakeBufferForPart(objMultiLines[k], objDSVsKirotei);
                            }

                            for (l = 0; l < 5; l++)
                            {
                                Marshal.ReleaseComObject(objDSVsKirotei[l]);
                                objDSVsKirotei[l] = null;
                            }
                            Marshal.ReleaseComObject(objLineTmp);
                            objLineTmp = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        objSubRS.Close();
                        Marshal.ReleaseComObject(objSubRS);
                        objSubRS = null;

                        throw new Exception(ex.Message);
                    }
                }

                //objSubRS.Close();
                //Marshal.ReleaseComObject(objSubRS);
                //objSubRS = null;
                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }

            if (objRS != null)
            {
                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        #endregion
    }
}
